<?php

/**
 * 订单客户信息
 * 模型对应数据库表 "orders_customer".
 *
 * 以下是可用表orders_customer的栏位:
 * @property integer $id
 * @property string $order_number
 * @property string $first_name
 * @property string $last_name
 * @property string $email
 * @property string $ip
 * @property string $phone
 * @property string $country
 * @property string $city
 * @property string $address
 * @property string $msn
 */
class OrdersCustomer extends CActiveRecord {

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return OrdersCustomer the static model class
     * @author nirsh
     * @example
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'orders_customer';
    }

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('order_number', 'required'),
            array('order_number, ip, phone', 'length', 'max' => 20),
            array('first_name, last_name, email, country, city', 'length', 'max' => 50),
            array('address, msn', 'length', 'max' => 255),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, order_number, first_name, last_name, email, ip, phone, country, city, address, msn', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'order_number' => '订单编号',
            'first_name' => '姓',
            'last_name' => '名',
            'email' => 'Email',
            'ip' => 'Ip',
            'phone' => '电话',
            'country' => '国家',
            'city' => '城市',
            'address' => '地址',
            'msn' => 'Msn',
        );
    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('order_number', $this->order_number);
        $criteria->compare('first_name', $this->first_name, true);
        $criteria->compare('last_name', $this->last_name, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('ip', $this->ip, true);
        $criteria->compare('phone', $this->phone, true);
        $criteria->compare('country', $this->country, true);
        $criteria->compare('city', $this->city, true);
        $criteria->compare('address', $this->address, true);
        $criteria->compare('msn', $this->msn, true);

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

}